/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package connection;

import constan.Config;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author LINHNC
 */
public class AppConnection {

    // server info config
    
    private Socket connection;
    private DataOutputStream outStream;
    private BufferedReader inStream;
    private boolean isFinish = false;

    private boolean isConnected = false;
    
    public AppConnection() {
        try {
            connection = new Socket(Config.SEVER_IP, Config.SERVER_PORT);
            inStream = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            outStream = new DataOutputStream(connection.getOutputStream());

            // display infor connection :
            if (connection.isConnected()) {
                isConnected = true;
                System.out.println("Connect to server: " + Config.SEVER_IP);
            } else {
                System.out.println("can't connect to: " + Config.SEVER_IP + "; port:" + Config.SERVER_PORT);
            }
        } catch (UnknownHostException ex) {
            isConnected = false;
            Logger.getLogger(AppConnection.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            isConnected = false;
            Logger.getLogger(AppConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void sendData() {
        try {
            BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
            String input = "";

            do {
                System.out.println("input text to send (input \"quit\" to exit):");
                input = keyboard.readLine();
                outStream.writeBytes(input);
                outStream.write(13);
                outStream.write(10);

                outStream.flush();
                if (input.equalsIgnoreCase("quit")) {
                    isFinish = true;
                }
            } while (!isFinish);
        } catch (IOException ex) {
            Logger.getLogger(AppConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void receiveData() {
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                int i = 30;  //limit wait time;
                while ((connection == null || inStream == null) && i > 0) {
                    try {
                        i--;
                        Thread.sleep(100);
                        System.out.println("wait for init socket! i=" + i);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(AppConnection.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                if (connection == null || inStream == null) {
                    return;
                }
                int byteReceiver;
                do {
                    try {
                        // receiver from server;
                        System.out.println(">>>>");
                        while ((byteReceiver = inStream.read()) != '\n') {
                            System.out.write(byteReceiver);
                        }
                        System.out.println();
                    } catch (IOException ex) {
                        isFinish = true;
                    }
                } while (!isFinish);
                shutdown();
            }
        });
        t.start();
    }

    public void shutdown() {
        try {
            connection.close();
            System.out.println("socket is shutdown()!");
        } catch (IOException ex) {
            Logger.getLogger(AppConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
